﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using VASJ.Service.Library;

namespace VASJ.Service.Utils
{
    public class MembershipHelper
    {
        public static void CreateRoles()
        {
            try
            {
                string newRoles = ConfigurationManager.AppSettings["CSRoles"];
                if (newRoles == null)
                    return;
                //UserRoleManager manager = new UserRoleManager();
                foreach (string tmp in newRoles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    string role = tmp.Trim();
                    if (Roles.RoleExists(role))
                        continue;

                    Roles.CreateRole(role);
                    //string name = role.Substring(3);
                    //name = name.Substring(0, 1).ToUpper() + name.Substring(1);
                    //manager.AddRole(1, name, 1, role);
                }
            }
            catch (System.Exception ex)
            {
                Logger.Log.Fatal("Unable to create default roles");
                Logger.Log.Fatal(ex.Message);
            }
        }

        public static void CreateAdmin()
        {
            try
            {
                string adminUsername = ConfigurationManager.AppSettings["CSAdmin"];

                MembershipUser mu = Membership.GetUser(adminUsername);

                if (Membership.GetUser(adminUsername) == null)
                {
                    string[] roles = Roles.GetAllRoles();
                    if (roles.Length == 0)
                        return;
                    MembershipCreateStatus status;
                    var t = Membership.CreateUser(adminUsername, adminUsername, "support@vianova.fi", null, null, true, out status);
                    if (status == MembershipCreateStatus.Success)
                    {
                        foreach (string role in roles)
                            Roles.AddUserToRole(t.UserName, role);
                    }
                }
                else
                {
                    if (mu.IsLockedOut) mu.UnlockUser();
                }
            }
            catch (System.Exception ex)
            {
                Logger.Log.Fatal("Unable to create default admin");
                Logger.Log.Fatal(ex.Message);
            }
        }

        public static Guid GetUserId(System.Security.Principal.IPrincipal user)
        {
            MembershipUser member = Membership.GetUser(user.Identity.Name);
            return Guid.Parse(member.ProviderUserKey.ToString());
        }

        public static Guid? GetUserId(string username)
        {
            MembershipUser member = Membership.GetUser(username);
            if (member != null)
            {
                try
                {
                    return Guid.Parse(member.ProviderUserKey.ToString());
                }
                catch (Exception ex)
                {
                    Logger.Log.Fatal("Unable to get user");
                    Logger.Log.Fatal(ex.Message);
                }
            }
            return null;
        }

        public static bool CheckAdmin(System.Security.Principal.IPrincipal user)
        {
            return user.IsInRole(CS4Roles.CSAdmin) || user.Identity.Name == "sa";
        }

        public static bool CheckAdmin(string user)
        {
            return Roles.IsUserInRole(user, CS4Roles.CSAdmin) || user == "sa";
        }
    }

    public class CS4Roles
    {
        public const string CSAdmin = "cs_admin";
        public const string CSManager = "cs_manager";
        public const string CSUser = "cs_user";
        public const string CSWebUser = "cs_webuser";
    }
}